<template lang="pug">
.module-manage.app-container
  .tree-wrap
    el-tree(:data="state.listData" style="font-size: 14px" node-key="id" default-expand-all :expand-on-click-node="true" :props="state.defaultProps").tree-list-wrap
      template(#default="{ node, data }")
        .custom-tree-node
          span {{node.label}}
          .btn-list
            span(@click.stop="handleAdd(data)").span.span-add.el-icon-circle-plus
            span(@click.stop="handleEdit(data)").span.span-add.el-icon-edit
            span(@click.stop="handleDel(data)" v-if="data.id !== 1").span.span-add.el-icon-delete

  moduleManageAdd(v-model:isDialog="addShow" v-model:dialogRecord="state.defaultData" @closeFun="handleColse")
  moduleManageEdit(v-model:isDialog="editShow" v-model:dialogRecord="state.defaultData" @closeFun="handleColse")
</template>

<script setup>
import moduleManageAdd from './moduleManageAdd.vue'
import moduleManageEdit from './moduleManageEdit.vue'

import { getModule } from '../../api/user'

import { ref, reactive, toRefs, getCurrentInstance } from 'vue'

const { proxy } = getCurrentInstance() // 内部组件实例

// 数据
const state = reactive({
  listData: [], // 列表数据
  defaultProps: {
    children: 'childList',
    label: 'name'
  },
  defaultData: {}
})
const total = ref(60)
const tableLoading = ref(false)
const addShow = ref(false)
const editShow = ref(false)

getList()

// 获取列表
async function getList () {
  // tableLoading.value = true
  // const getData = await getModule(state.pageData)

  state.listData = [{
    id: 1,
    parentId: 0,
    name: 'vue3.0 权限管理',
    level: 1,
    childList: [
      {
        id: 2,
        parentId: 1,
        name: '系统管理',
        level: 2,
        childList: [
          {
            id: 3,
            parentId: 2,
            name: '用户管理',
            level: 3,
            childList: [
              { id: 4, parentId: 3, name: '用户管理-添加', level: 4, childList: [] }
            ]
          },
          { id: 5, parentId: 2, name: '角色管理', level: 3, childList: [] },
          { id: 6, parentId: 2, name: '模块管理', level: 3, childList: [] }
        ]
      },
      {
        id: 7,
        parentId: 1,
        name: '落地页管理',
        level: 2,
        childList: [
          { id: 8, parentId: 7, name: '落地页列表', level: 3, childList: [] }
        ]
      }
    ]
  }]
}

// 编辑
function handleEdit (row) {
  state.defaultData = row
  editShow.value = true
}

// 权限设置
function handleAdd (row) {
  state.defaultData = row
  addShow.value = true
}

// 删除列表
function handleDel (row) {
  proxy.$confirm('确定删除？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    proxy.$message({ type: 'success', message: '删除成功' })
  }).catch(() => {
    proxy.$message({ type: 'warning', message: '已取消删除' })
  })
}

// 关闭弹窗
function handleColse (val) {
  console.log('close')
}
</script>

<style lang="scss">
  .module-manage {
    color: #666;
    margin: 0 0 20px 0;
    .tree-wrap {
      .custom-tree-node {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .btn-list {
          display: none;
          align-items: center;
          justify-content: space-between;
          padding-right: 5px;
          .span {
            min-width: 25px;
            width: 25px;
            height: 25px;
            margin-left: 5px;
            font-size: 20px;
            &.el-icon-delete {
              font-size: 18px;
            }
          }
        }
      }
      .custom-tree-node:hover {
        .btn-list {
          display: flex;
        }
      }
    }
    .el-tree-node__content>.el-tree-node__expand-icon {
      margin-top: 0;
    }
    .tree-list-wrap .el-tree-node__content {
      line-height: 45px;
      height: 45px;
      display: flex;
      align-items: center;
    }
  }
</style>
